/*******************************************************************************
 * Copyright (c) 2011 LegSem.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.txt
 * 
 * Contributors:
 *     LegSem - initial API and implementation
 ******************************************************************************/
package com.legstar.test.coxb.t1contxt.bind;

import com.legstar.coxb.ICobolBinding;
import com.legstar.coxb.common.CComplexBinding;
import com.legstar.coxb.ICobolStringBinding;
import com.legstar.coxb.CobolBindingFactory;
import com.legstar.coxb.ICobolBindingFactory;
import com.legstar.coxb.ICobolPackedDecimalBinding;
import com.legstar.coxb.ICobolBinaryBinding;
import com.legstar.coxb.ICobolComplexBinding;
import com.legstar.coxb.host.HostException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.legstar.test.coxb.t1contxt.Dfhcommarea;
import com.legstar.test.coxb.t1contxt.ObjectFactory;

/**
 * LegStar Binding for Complex element :
 *   Dfhcommarea.
 * 
 * This class was generated by LegStar Binding generator.
 */
public class DfhcommareaBinding 
             extends CComplexBinding {

    /** Value object to which this cobol complex element is bound. */
    private Dfhcommarea mValueObject;
  
    /** Indicates that the associated Value object just came from the constructor
     * and doesn't need to be recreated. */
    private boolean mUnusedValueObject = false;
    
    /** Maximum host bytes size for this complex object. */
    private static final int BYTE_LENGTH = 24;
    
    /** Child bound to value object property ComUserId(String). */
    public ICobolStringBinding _comUserId;
    /** Child bound to value object property ComTransactionId(String). */
    public ICobolStringBinding _comTransactionId;
    /** Child bound to value object property ComTaskId(Integer). */
    public ICobolPackedDecimalBinding _comTaskId;
    /** Child bound to value object property ComTermId(String). */
    public ICobolStringBinding _comTermId;
    /** Child bound to value object property ComCommareaSize(Long). */
    public ICobolBinaryBinding _comCommareaSize;
            
    /** Logger. */
    private final Log _log = LogFactory.getLog(getClass());

    /** Binding factory. */
    private static final ICobolBindingFactory BF
        = CobolBindingFactory.getBindingFactory();
 
    /** Static reference to Value object factory to be used as default. */
    private static final ObjectFactory JF = new ObjectFactory();
    
    /** Current Value object factory (Defaults to the static one but can be
     *  changed). */
    private ObjectFactory mValueObjectFactory = JF;
    
    /**
     * Constructor for a root Complex element without a bound Value object.
     */
    public DfhcommareaBinding() {
        this(null);
    }

    /**
     * Constructor for a root Complex element with a bound Value object.
     * 
     * @param valueObject the concrete Value object instance bound to this
     *        complex element
     */
    public DfhcommareaBinding(
            final Dfhcommarea valueObject) {
        this("", "", null, valueObject);
    }

    /**
    * Constructor for a Complex element as a child of another element and
    * an associated Value object.
    * 
    * @param bindingName the identifier for this binding
    * @param fieldName field name in parent Value object
    * @param valueObject the concrete Value object instance bound to this
    *        complex element
    * @param parentBinding a reference to the parent binding
    */
    public DfhcommareaBinding(
            final String bindingName,
            final String fieldName,
            final ICobolComplexBinding parentBinding,
            final Dfhcommarea valueObject) {
        
        super(bindingName, fieldName, Dfhcommarea.class, null, parentBinding);
        mValueObject = valueObject;
        if (mValueObject != null) {
            mUnusedValueObject = true;
        }
        initChildren();
        setByteLength(BYTE_LENGTH);
    }

    /** Creates a binding property for each child. */
    private void initChildren() {
        if (_log.isDebugEnabled()) {
            _log.debug("Initializing started");
        }
        /* Create binding children instances */

        _comUserId = BF.createStringBinding("ComUserId",
               "ComUserId", String.class, this);
        _comUserId.setCobolName("COM-USER-ID");
        _comUserId.setByteLength(8);
        _comTransactionId = BF.createStringBinding("ComTransactionId",
               "ComTransactionId", String.class, this);
        _comTransactionId.setCobolName("COM-TRANSACTION-ID");
        _comTransactionId.setByteLength(4);
        _comTaskId = BF.createPackedDecimalBinding("ComTaskId",
               "ComTaskId", Integer.class, this);
        _comTaskId.setCobolName("COM-TASK-ID");
        _comTaskId.setByteLength(4);
        _comTaskId.setTotalDigits(7);
        _comTaskId.setIsSigned(true);
        _comTermId = BF.createStringBinding("ComTermId",
               "ComTermId", String.class, this);
        _comTermId.setCobolName("COM-TERM-ID");
        _comTermId.setByteLength(4);
        _comCommareaSize = BF.createBinaryBinding("ComCommareaSize",
               "ComCommareaSize", Long.class, this);
        _comCommareaSize.setCobolName("COM-COMMAREA-SIZE");
        _comCommareaSize.setByteLength(4);
        _comCommareaSize.setTotalDigits(8);

        /* Add children to children list */
        getChildrenList().add(_comUserId);
        getChildrenList().add(_comTransactionId);
        getChildrenList().add(_comTaskId);
        getChildrenList().add(_comTermId);
        getChildrenList().add(_comCommareaSize);
 
        if (_log.isDebugEnabled()) {
            _log.debug("Initializing successful");
        }
    }
    
    /** {@inheritDoc} */
    public void createValueObject() throws HostException {
        /* Since this complex binding has a constructor that takes a
         * Value object, we might already have a Value object that
         * was not used yet. */
        if (mUnusedValueObject && mValueObject != null) {
            mUnusedValueObject = false;
            return;
        }
        mValueObject = mValueObjectFactory.createDfhcommarea();
    }

    /** {@inheritDoc} */
    public void setChildrenValues() throws HostException {

         /* Make sure there is an associated Value object*/
        if (mValueObject == null) {
            createValueObject();
        }
        /* Get Value object property _comUserId */
        if (_log.isDebugEnabled()) {
            _log.debug("Getting value from Value object property "
                    + "_comUserId"
                    + " value=" + mValueObject.getComUserId());
        }
        _comUserId.setObjectValue(mValueObject.getComUserId());
        /* Get Value object property _comTransactionId */
        if (_log.isDebugEnabled()) {
            _log.debug("Getting value from Value object property "
                    + "_comTransactionId"
                    + " value=" + mValueObject.getComTransactionId());
        }
        _comTransactionId.setObjectValue(mValueObject.getComTransactionId());
        /* Get Value object property _comTaskId */
        if (_log.isDebugEnabled()) {
            _log.debug("Getting value from Value object property "
                    + "_comTaskId"
                    + " value=" + mValueObject.getComTaskId());
        }
        _comTaskId.setObjectValue(mValueObject.getComTaskId());
        /* Get Value object property _comTermId */
        if (_log.isDebugEnabled()) {
            _log.debug("Getting value from Value object property "
                    + "_comTermId"
                    + " value=" + mValueObject.getComTermId());
        }
        _comTermId.setObjectValue(mValueObject.getComTermId());
        /* Get Value object property _comCommareaSize */
        if (_log.isDebugEnabled()) {
            _log.debug("Getting value from Value object property "
                    + "_comCommareaSize"
                    + " value=" + mValueObject.getComCommareaSize());
        }
        _comCommareaSize.setObjectValue(mValueObject.getComCommareaSize());
     }

    /** {@inheritDoc} */
    public void setPropertyValue(final int index) throws HostException {
 
        ICobolBinding child = getChildrenList().get(index);
        
       /* Children that are not bound to a value object are ignored.
        * This includes Choices and dynamically generated counters
        * for instance.  */
        if (!child.isBound()) {
            return;
        }
        
        /* Set the Value object property value from binding object */
        Object bindingValue = null;
        switch (index) {
        case 0:
            bindingValue = child.getObjectValue(String.class);
            mValueObject.setComUserId((String) bindingValue);
            break;
        case 1:
            bindingValue = child.getObjectValue(String.class);
            mValueObject.setComTransactionId((String) bindingValue);
            break;
        case 2:
            bindingValue = child.getObjectValue(Integer.class);
            mValueObject.setComTaskId((Integer) bindingValue);
            break;
        case 3:
            bindingValue = child.getObjectValue(String.class);
            mValueObject.setComTermId((String) bindingValue);
            break;
        case 4:
            bindingValue = child.getObjectValue(Long.class);
            mValueObject.setComCommareaSize((Long) bindingValue);
            break;
         default:
            break;
        }
        if (_log.isDebugEnabled()) {
            _log.debug("Setting value of Value object property "
                    + child.getJaxbName()
                    + " value=" + bindingValue);
        }
    }

    /** {@inheritDoc} */
    public Object getObjectValue(
            final Class < ? > type) throws HostException {
        if (type.equals(Dfhcommarea.class)) {
            return mValueObject;
        } else {
            throw new HostException("Attempt to get binding " + getBindingName()
                    + " as an incompatible type " + type);
        }
    }

    /** {@inheritDoc} */
    public void setObjectValue(
            final Object bindingValue) throws HostException {
        if (bindingValue == null) {
            mValueObject = null;
            return;
        }
        if (bindingValue.getClass().equals(Dfhcommarea.class)) {
            mValueObject = (Dfhcommarea) bindingValue;
        } else {
            throw new HostException("Attempt to set binding " + getBindingName()
                    + " from an incompatible value " + bindingValue);
        }
    }

    /**
     * @return the java object factory for objects creation
     */
    public ObjectFactory getObjectFactory() {
        return mValueObjectFactory;
    }

    /**
     * @param valueObjectFactory the java object factory for objects creation 
     */
    public void setObjectFactory(final Object valueObjectFactory) {
        mValueObjectFactory = (ObjectFactory) valueObjectFactory;
    }

    /** {@inheritDoc} */
    public boolean isSet() {
        return (mValueObject != null);
    }

    /**
     * @return the bound Value object
     */
    public Dfhcommarea getDfhcommarea() {
        return mValueObject;
    }
    
    /**
     * The COBOL complex element maximum length in bytes.
     * 
     * @return COBOL complex element maximum length in bytes
     */
    public int getByteLength() {
        return BYTE_LENGTH;
    }
}

